﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using static System.Net.WebRequestMethods;
using File = System.IO.File;

namespace dxf_Remove_ok
{
    internal class Program
    {

        private static Regex regOkOnly = new Regex(@"\s*ok\s*", RegexOptions.IgnoreCase);

        static Regex regWithOk = new Regex(@".+\s*ok\s*$", RegexOptions.IgnoreCase);

        static bool ContainsOk(string s)
        {
            return regWithOk.IsMatch(s);
        }

        public static string RemoveOk(string s)
        {
            if (regOkOnly.IsMatch(s))
                return regOkOnly.Replace(s, "");
            else
                return s;
        }

        static void FoldersRemoveOk(string folder)
        {
            foreach (string oldFull in Directory.GetDirectories(folder))
            {
                FoldersRemoveOk(oldFull);
                string parentFolder = Path.GetDirectoryName(oldFull);
                string folderName = Path.GetFileName(oldFull);

                if (ContainsOk(folderName))
                {
                    string newFolderName = RemoveOk(folderName);
                    string newFull = Path.Combine(parentFolder, newFolderName);
                    try
                    {
                        Directory.Move(oldFull, newFull);
                        Console.WriteLine($"{oldFull} --> {newFull}");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"{oldFull} {ex.Message}");
                    }
                }
            }
        }

        static void FilesRemoveOk(string searchDir)
        {
            foreach (string file in Directory.GetFiles(searchDir, "*.dxf", SearchOption.AllDirectories))
            {
                string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
                if (ContainsOk(file))
                {
                    string newFileNameWithoutExtension = RemoveOk(fileNameWithoutExtension) + ".dxf";

                    string folder = Path.GetDirectoryName(file);
                    string newFile = Path.Combine(folder, newFileNameWithoutExtension);
                    try
                    {
                        File.Move(file, newFile);
                        Console.WriteLine($"{file} --> {newFile}");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"{file} {ex.Message}");
                    }
                }
            }
        }


        static void Main(string[] args)
        {
            string searchDir = Environment.CurrentDirectory;
            Console.WriteLine($"搜索文件夹:{searchDir}");
            FilesRemoveOk(searchDir);
            FoldersRemoveOk(searchDir);
            Console.WriteLine("任意键退出。");
            Console.ReadKey();
        }
    }
}
